# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
data = np.mat([[0.697,0.460,1],
        [0.774,0.376,1],
        [0.634,0.264,1],
        [0.608,0.318,1],
        [0.556,0.215,1],
        [0.403,0.237,1],
        [0.481,0.149,1],
        [0.437,0.211,1],
        [0.666,0.091,0],
        [0.243,0.267,0],
        [0.245,0.057,0],
        [0.343,0.099,0],
        [0.639,0.161,0],
        [0.657,0.198,0],
        [0.360,0.370,0],
        [0.593,0.042,0],
        [0.719,0.103,0]]) # 2 attributes(column),17 instances(row)
data=data[:,0:2]
X1=data[0:8,:]
X2=data[8:,:]
miu1=np.mean(X1,axis=0)
miu2=np.mean(X2,axis=0)
S1=np.zeros(4).reshape(2,2)
S2=np.zeros(4).reshape(2,2)
for x1 in X1:
    S1=S1+(x1-miu1).T*(x1-miu1)
for x2 in X2:
    S2=S2+(x2-miu2).T*(x2-miu2)
Sw=np.mat(S1+S2)
Swinv=np.linalg.inv(Sw)
w=np.mat(Swinv*(miu1-miu2).T)
X1=X1.getA()
X2=X2.getA()
plt.scatter(X1[:,0],X1[:,1],label='+')
plt.scatter(X2[:,0],X2[:,1],label='-')
plt.plot([0,1],[0,-w[0]/w[1]],label='y')
plt.legend()
plt.show()
